package com.ruoyi.web.controller.process;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.pdf.PdfExporter;
import com.ruoyi.process.domain.UnqualifyHandle;
import com.ruoyi.process.service.IUnqualifyHandleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 不合格食品处置Controller
 *
 * @author wng
 * @date 2024-08-04
 */
@RestController
@RequestMapping("/process/unqualifyHandle")
public class UnqualifyHandleController extends BaseController {
    @Resource
    private IUnqualifyHandleService unqualifyHandleService;

    /**
     * 查询不合格食品处置列表
     */
    @PreAuthorize("@ss.hasPermi('process:unqualifyHandle:list')")
    @GetMapping("/list")
    public TableDataInfo list(UnqualifyHandle unqualifyHandle) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (loginUser.getUserId() != 1 && unqualifyHandle.getDeptId() == null) {
            unqualifyHandle.setDeptId(loginUser.getDeptId());
        }
        List<UnqualifyHandle> list = unqualifyHandleService.selectUnqualifyHandleList(unqualifyHandle);
        return getDataTable(list);
    }

    /**
     * 导出不合格食品处置列表
     */
    @PreAuthorize("@ss.hasPermi('process:unqualifyHandle:export')")
    @Log(title = "不合格食品处置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UnqualifyHandle unqualifyHandle) throws Exception{
        List<UnqualifyHandle> list = unqualifyHandleService.selectUnqualifyHandleList(unqualifyHandle);
//        ExcelUtil<UnqualifyHandle> util = new ExcelUtil<UnqualifyHandle>(UnqualifyHandle.class);
//        util.exportExcel(response, list, "不合格食品处置数据");
        PdfExporter<UnqualifyHandle> util = new PdfExporter<>();
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=export.pdf");
        util.exportListToPdf(list,response.getOutputStream());
    }

    /**
     * 获取不合格食品处置详细信息
     */
    @PreAuthorize("@ss.hasPermi('process:unqualifyHandle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(unqualifyHandleService.selectUnqualifyHandleById(id));
    }

    /**
     * 新增不合格食品处置
     */
    @PreAuthorize("@ss.hasPermi('process:unqualifyHandle:add')")
    @Log(title = "不合格食品处置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UnqualifyHandle unqualifyHandle) {
        LoginUser loginUser = getLoginUser();
        unqualifyHandle.setCreateBy(String.valueOf(loginUser.getUserId()));
        unqualifyHandle.setDeptId(loginUser.getDeptId());
        return toAjax(unqualifyHandleService.insertUnqualifyHandle(unqualifyHandle));
    }

    /**
     * 修改不合格食品处置
     */
    @PreAuthorize("@ss.hasPermi('process:unqualifyHandle:edit')")
    @Log(title = "不合格食品处置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UnqualifyHandle unqualifyHandle) {
        LoginUser loginUser = getLoginUser();
        unqualifyHandle.setCreateBy(String.valueOf(loginUser.getUserId()));
        return toAjax(unqualifyHandleService.updateUnqualifyHandle(unqualifyHandle));
    }

    /**
     * 删除不合格食品处置
     */
    @PreAuthorize("@ss.hasPermi('process:unqualifyHandle:remove')")
    @Log(title = "不合格食品处置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(unqualifyHandleService.deleteUnqualifyHandleByIds(ids));
    }
}
